home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / raytrace / pov / gen / pvcoil21 / coil_v20.c next >
Encoding:
C/C++ Source or Header  |  1992-07-25  |  4.9 KB  |  187 lines

  1. /*
  2.  Program : Coil
  3.  Purpose : Create coil objects for PoV Ray v1.0 raytracer
  4.  Created : 7/25/92
  5.  By      : Bill Kirby CIS [70711,2407]
  6.  File    : Coil.c
  7.  Compiler: Borland C++ v3.1
  8.  Model   : Small
  9.  Comments: Used to create twisted coil object made with spheres
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <math.h>
  16.  
  17. #ifndef PI
  18. #define PI 3.1415926535897932384626
  19. #endif
  20.  
  21. #ifndef TRUE
  22. #define TRUE 1
  23. #define FALSE 0
  24. #endif
  25.  
  26. void show_title(void);
  27. void get_inputs(void);
  28. void write_header(char *filename, char *union_name);
  29. void write_piece(double xpos, double ypos, double zpos, double radius);
  30. void write_end(void);
  31. void err_exit(char *message);
  32.  
  33. FILE *outfile;
  34. char filename[80],union_name[80],buff[256];
  35. double x1,y1,x2,y2,z2,xpos,ypos,zpos,Rad1,rad2,radius;
  36. double angle1,angle2,k;
  37. long i,j,Ntwist,Ntube,steps;
  38. double xmax = -1e300, xmin = 1e300, ymax = -1e300, ymin = 1e300, zmax = -1e300, zmin = 1e300;
  39.  
  40. main(argc,argv)
  41. int argc;  char *argv[];
  42. {
  43.  
  44.     show_title();
  45.  
  46. /* Get coil values from user */
  47.     get_inputs();
  48.  
  49. /* Open file for output and write header */
  50.     printf("\n\nCreating data file %s\n",filename);
  51.     if ((outfile = fopen(filename,"w")) == NULL)
  52.      err_exit("Opening file.");
  53.  
  54.     write_header(filename,union_name);
  55.  
  56. /* Compute twisted coil object */
  57.  
  58.     for(i=0;i<steps;++i){
  59.       angle1 = 2 * PI * Ntube * (double)i/ (double)steps;
  60.       x1 = cos( angle1 );
  61.       y1 = sin( angle1 );
  62.       angle2 = (double)( Ntwist + 1.0/Ntube) * angle1;
  63.       x2 = cos( angle2 );
  64.       z2 = sin( angle2);
  65.       xpos = k * ((Rad1 * x1) + (rad2 * x2 * x1));
  66.       ypos = k * ((Rad1 * y1) + (rad2 * x2 * y1));
  67.       zpos = k * rad2 * z2;
  68.       xmax = max(xmax,xpos);
  69.       xmin = min(xmin,xpos);
  70.       ymax = max(ymax,ypos);
  71.       ymin = min(ymin,ypos);
  72.       zmax = max(zmax,zpos);
  73.       zmin = min(zmin,zpos);
  74.       write_piece(xpos,ypos,zpos,radius);
  75.     }
  76.  
  77. /* Write object end and close file */
  78.     write_end();
  79.     fclose(outfile);
  80.     printf("\nFile %s created.\n",filename);
  81.     return(0);
  82. }
  83.  
  84. void show_title(void)
  85. {
  86.     printf("Coil v2.00\n");
  87.     printf("Creates a PoV-Ray v1.0 raytracer data file of a twisted coil object.\n");
  88.     printf("- W. D. Kirby 7/25/92\n\n");
  89. }
  90.  
  91. void get_inputs(void)
  92. {
  93. /* Get coil values from user */
  94.  
  95.     printf("Ouput filename? [coil.inc]: ");
  96.     gets(buff);
  97.     strcpy(filename,buff);
  98.  
  99.     printf("Union name? [coil]: ");
  100.     gets(buff);
  101.     strcpy(union_name,buff);
  102.  
  103.     printf("Number of spheres? [100]: ");
  104.     gets(buff);
  105.     steps = atoi(buff);
  106.  
  107.     printf("Number of spheres in cross-section? [2]: ");
  108.     gets(buff);
  109.     Ntube = atoi(buff);
  110.  
  111.     printf("Number of twists per revolution? [2]: ");
  112.     gets(buff);
  113.     Ntwist = atoi(buff);
  114.  
  115.     printf("Major radius? [1.0]: ");
  116.     gets(buff);
  117.     sscanf (buff, "%lf", &Rad1);
  118.  
  119.     printf("Minor radius? [0.25]: ");
  120.     gets(buff);
  121.     sscanf (buff, "%lf", &rad2);
  122.  
  123.     printf("Sphere radius? [0.25]: ");
  124.     gets(buff);
  125.     sscanf (buff, "%lf", &radius);
  126.  
  127.     printf("Overall scale value (k)? [1.0]: ");
  128.     gets(buff);
  129.     sscanf (buff, "%lf", &k);
  130.  
  131. /* Set up default values */
  132.     if(filename[0]=='\0') strcpy(filename,"coil.inc");
  133.     if(union_name[0]=='\0') strcpy(union_name,"coil");
  134.     if(steps==0) steps = 100;
  135.     if(Ntube==0) Ntube = 2;
  136.     if(Ntwist==0) Ntwist = 2;
  137.     if(Rad1==0.0) Rad1 = 1.0;
  138.     if(rad2==0.0) rad2 = 0.25;
  139.     if(radius==0.0) radius = 0.25;
  140.     if(k==0.0) k = 1.0;
  141. }
  142.  
  143. void write_header(char *filename,  char *union_name)
  144. {
  145.     fprintf(outfile,"// File : %s  Union Name : %s \n",filename,union_name);
  146.     fprintf(outfile,"// This data file created by COIL.EXE v2.0 for the PoV Ray v1.0 raytracer.\n");
  147.     fprintf(outfile,"\n//Twists=%ld Cross Section=%ld Spheres=%ld Scale=%lf \n\n",Ntwist,Ntube,steps,k);
  148.     fprintf(outfile,"declare %s = object {\n union {\n",union_name);
  149. }
  150.  
  151. void write_piece(double xpos, double ypos, double zpos,double radius)
  152. {
  153.  
  154.     fprintf(outfile,"      sphere { <%f %f %f> %f }\n",xpos,ypos,zpos,radius);
  155.  
  156. }
  157.  
  158. void write_end(void)
  159. {
  160.     fprintf(outfile," }\n\n");
  161.     xmax = 1.01 * (xmax + radius);
  162.     xmin = 1.01 * (xmin - radius);
  163.     ymax = 1.01 * (ymax + radius);
  164.     ymin = 1.01 * (ymin - radius);
  165.     zmax = 1.01 * (zmax + radius);
  166.     zmin = 1.01 * (zmin - radius);
  167.     fprintf(outfile,"bounded_by {\n");
  168.     fprintf(outfile,"  box { < %f %f %f > < %f %f %f> }\n",xmin,ymin,zmin,xmax,ymax,zmax);
  169.     fprintf(outfile,"}\n");
  170.     fprintf(outfile,"  texture {\n");
  171.     fprintf(outfile,"    ambient 0.3\n");
  172.     fprintf(outfile,"    diffuse 0.7\n");
  173.     fprintf(outfile,"    phong 1.0\n");
  174.     fprintf(outfile,"    phong_size 20.0\n");
  175.     fprintf(outfile,"    color red 1.0 green 0.0 blue 0.0  \n");
  176.     fprintf(outfile,"  }\n\n");
  177.     fprintf(outfile," }\n\n");
  178. }
  179.  
  180. void err_exit(char *message)
  181. {
  182.     puts("\n\nERROR! \a");
  183.     puts(message);
  184.     puts("- Exiting \n");
  185.     exit(1);
  186. }
  187.